{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import utils\n",
    "from facebook import Facebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "email: zhuamenfacebook@163.com\n",
      "password: sklois\n"
     ]
    }
   ],
   "source": [
    "email, password = utils.get_account(\"account.csv\", 0)\n",
    "print(\"email: %s\\npassword: %s\" % (email, password))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 初始化Facebook类\n",
    "fb = Facebook(email, password, \"Chrome\", False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# 登录\n",
    "fb.log_in()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "fb.get_friends_list(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['https://www.facebook.com/photo.php?fbid=126169088231378&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=960%2C632',\n",
       " 'https://www.facebook.com/photo.php?fbid=110531536461800&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=1200%2C791',\n",
       " 'https://www.facebook.com/photo.php?fbid=110531466461807&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=1200%2C675',\n",
       " 'https://www.facebook.com/photo.php?fbid=110531449795142&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=1200%2C675',\n",
       " 'https://www.facebook.com/photo.php?fbid=110531433128477&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=1200%2C750',\n",
       " 'https://www.facebook.com/photo.php?fbid=108757339972553&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=1800%2C1200',\n",
       " 'https://www.facebook.com/photo.php?fbid=103087587206195&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=1200%2C750',\n",
       " 'https://www.facebook.com/photo.php?fbid=103087207206233&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=606%2C606']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fb.get_photos_list(fb.homepage_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('https://scontent-nrt1-1.xx.fbcdn.net/v/t1.0-9/29793014_126169091564711_4485160203192369152_n.jpg?_nc_cat=0&oh=bf6bfad4cd74754c26892401deec761c&oe=5B6AC97B', '1522980982', [], '这样应该可以吧', '960', '632')\n"
     ]
    }
   ],
   "source": [
    "photo_info = fb.get_photo_info(\"https://www.facebook.com/photo.php?fbid=126169088231378&set=pb.100025148614740.-2207520000.1523121298.&type=3&size=960%2C632\")\n",
    "print(photo_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "image 29793014_126169091564711_4485160203192369152_n.jpg is saved successfully.\n",
      "image 29064468_110531536461800_8350038227599897795_o.jpg is saved successfully.\n",
      "image 29352256_110531466461807_2083264940520021768_o.jpg is saved successfully.\n",
      "image 29744728_110531449795142_3054763241779463875_o.jpg is saved successfully.\n",
      "image 29749426_110531433128477_8447009496841284357_o.jpg is saved successfully.\n",
      "image 29665516_108757339972553_4734022796668337620_o.jpg is saved successfully.\n",
      "image 29354757_103087587206195_738496168481783988_o.jpg is saved successfully.\n",
      "image 29541840_103087207206233_5502368042024210091_n.jpg is saved successfully.\n"
     ]
    }
   ],
   "source": [
    "fb.download_photos_one(fb.homepage_url, _folder_name=\"myself\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "def folder_make(_folder_name=\"./\"):\n",
    "    if _folder_name == \"./\":\n",
    "        pass\n",
    "    else:\n",
    "        if not os.path.exists(_folder_name):\n",
    "            os.mkdir(_folder_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pip\n",
    "from subprocess import call\n",
    "def get_packages():\n",
    "    _packages = list()\n",
    "    for distribution in pip.get_installed_distributions():\n",
    "        package_name = distribution.project_name\n",
    "        _packages.append(package_name)\n",
    "\n",
    "    return _packages\n",
    "\n",
    "\n",
    "def package_check(packages=None):\n",
    "    system_packages = get_packages()\n",
    "    if packages is None:\n",
    "        packages = [\"selenium\", \"beautifulsoup4\"]\n",
    "    if isinstance(packages, list) is False:\n",
    "        packages = [packages]\n",
    "    for package in packages:\n",
    "        if package not in system_packages:\n",
    "            call(\"pip install \" + package, shell=True)\n",
    "            print(\"%s is installed successfully.\" % package)\n",
    "        else:\n",
    "            pass\n",
    "    print(\"Packages check completed.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver import Remote\n",
    "from selenium.webdriver.chrome import options\n",
    "from selenium.common.exceptions import InvalidArgumentException\n",
    "\n",
    "class ReuseChrome(Remote):\n",
    "\n",
    "    def __init__(self, command_executor, session_id):\n",
    "        self.r_session_id = session_id\n",
    "        Remote.__init__(self, command_executor=command_executor, desired_capabilities={})\n",
    "\n",
    "    def start_session(self, capabilities, browser_profile=None):\n",
    "        \"\"\"\n",
    "        重写start_session方法\n",
    "        \"\"\"\n",
    "        if not isinstance(capabilities, dict):\n",
    "            raise InvalidArgumentException(\"Capabilities must be a dictionary\")\n",
    "        if browser_profile:\n",
    "            if \"moz:firefoxOptions\" in capabilities:\n",
    "                capabilities[\"moz:firefoxOptions\"][\"profile\"] = browser_profile.encoded\n",
    "            else:\n",
    "                capabilities.update({'firefox_profile': browser_profile.encoded})\n",
    "\n",
    "        self.capabilities = options.Options().to_capabilities()\n",
    "        self.session_id = self.r_session_id\n",
    "        self.w3c = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2566fb48126de845607ccd29dae8e64e\n",
      "http://127.0.0.1:61920\n",
      "http://www.spiderpy.cn/\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "#  第一次使用Chrome() 新建浏览器会话\n",
    "driver = webdriver.Chrome()\n",
    "\n",
    "# 记录 executor_url 和 session_id 以便复用session\n",
    "executor_url = driver.command_executor._url\n",
    "session_id = driver.session_id\n",
    "# 访问百度\n",
    "driver.get(\"http://www.spiderpy.cn/\")\n",
    "\n",
    "print(session_id)\n",
    "print(executor_url)\n",
    "\n",
    "# 假如driver对象不存在，但浏览器未关闭\n",
    "del driver\n",
    "\n",
    "# 使用ReuseChrome()复用上次的session\n",
    "driver2 = ReuseChrome(command_executor=executor_url, session_id=session_id)\n",
    "\n",
    "# 打印current_url为百度的地址，说明复用成功\n",
    "print(driver2.current_url)\n",
    "driver2.get(\"https://www.baidu.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nihao is installed successfully.\n",
      "Packages check completed.\n"
     ]
    }
   ],
   "source": [
    "package_check(\"nihao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import csv\n",
    "from random import randint\n",
    "def get_account(_account_file, number=None):\n",
    "    if not os.path.exists(_account_file):\n",
    "        print(\"The current file is not existed.\")\n",
    "    else:\n",
    "        account_list = list()\n",
    "        account_total_number = 0\n",
    "        with open(_account_file, \"r\") as file:\n",
    "            contents = csv.reader(file)\n",
    "            for content in contents:\n",
    "                account_list.append(content)\n",
    "                account_total_number += 1\n",
    "        if number is None:\n",
    "            number = randint(0, account_total_number-1)\n",
    "            \n",
    "    return account_list[number][0], account_list[number][1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "zhuamenfacebook@163.com sklois\n"
     ]
    }
   ],
   "source": [
    "account, password = get_account(\"account.csv\")\n",
    "print(account, password)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "ename": "WebDriverException",
     "evalue": "Message: chrome not reachable\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mWebDriverException\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-28-f2530de72854>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mfb\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_cookies\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mget_cookies\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    823\u001b[0m             \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_cookies\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    824\u001b[0m         \"\"\"\n\u001b[1;32m--> 825\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET_ALL_COOKIES\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'value'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    826\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    827\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mget_cookie\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    310\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    311\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 312\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    313\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    314\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    240\u001b[0m                 \u001b[0malert_text\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malert_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 242\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mWebDriverException\u001b[0m: Message: chrome not reachable\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n"
     ]
    }
   ],
   "source": [
    "fb.driver.get_cookies()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver import Remote\n",
    "from selenium.webdriver.chrome import options\n",
    "from selenium.common.exceptions import InvalidArgumentException\n",
    "\n",
    "class ReuseChrome(Remote):\n",
    "\n",
    "    def __init__(self, command_executor, session_id):\n",
    "        self.r_session_id = session_id\n",
    "        Remote.__init__(self, command_executor=command_executor, desired_capabilities={})\n",
    "\n",
    "    def start_session(self, capabilities, browser_profile=None):\n",
    "        \"\"\"\n",
    "        重写start_session方法\n",
    "        \"\"\"\n",
    "        if not isinstance(capabilities, dict):\n",
    "            raise InvalidArgumentException(\"Capabilities must be a dictionary\")\n",
    "        if browser_profile:\n",
    "            if \"moz:firefoxOptions\" in capabilities:\n",
    "                capabilities[\"moz:firefoxOptions\"][\"profile\"] = browser_profile.encoded\n",
    "            else:\n",
    "                capabilities.update({'firefox_profile': browser_profile.encoded})\n",
    "\n",
    "        self.capabilities = options.Options().to_capabilities()\n",
    "        self.session_id = self.r_session_id\n",
    "        self.w3c = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "822c40659412e65edb0cca90f20625bf\n",
      "http://127.0.0.1:55448\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "#  第一次使用Chrome() 新建浏览器会话\n",
    "driver = webdriver.Chrome()\n",
    "\n",
    "# 记录 executor_url 和 session_id 以便复用session\n",
    "executor_url = driver.command_executor._url\n",
    "session_id = driver.session_id\n",
    "# 访问百度\n",
    "driver.get(\"http://www.spiderpy.cn/\")\n",
    "\n",
    "print(session_id)\n",
    "print(executor_url)\n",
    "\n",
    "# 假如driver对象不存在，但浏览器未关闭\n",
    "del driver"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "WebDriverException",
     "evalue": "Message: chrome not reachable\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mWebDriverException\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-12-97c30021bb9f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;31m# 打印current_url为百度的地址，说明复用成功\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcurrent_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      6\u001b[0m \u001b[0mdriver2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"https://www.baidu.com\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mcurrent_url\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    658\u001b[0m             \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcurrent_url\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    659\u001b[0m         \"\"\"\n\u001b[1;32m--> 660\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET_CURRENT_URL\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'value'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    661\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    662\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    310\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    311\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 312\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    313\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    314\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    240\u001b[0m                 \u001b[0malert_text\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malert_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 242\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mWebDriverException\u001b[0m: Message: chrome not reachable\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n"
     ]
    }
   ],
   "source": [
    "# 使用ReuseChrome()复用上次的session\n",
    "driver2 = ReuseChrome(command_executor=executor_url, session_id=session_id)\n",
    "\n",
    "# 打印current_url为百度的地址，说明复用成功\n",
    "print(driver2.current_url)\n",
    "driver2.get(\"https://www.baidu.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<__main__.ReuseChrome (session=\"822c40659412e65edb0cca90f20625bf\")>\n"
     ]
    }
   ],
   "source": [
    "print(driver2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "H_PS_PSSID 1428_21113_20927\n",
      "BAIDUID 6D13FC2B3EAEEDAE122B34C5FD8AF3DA:FG=1\n",
      "PSTM 1523169331\n",
      "BIDUPSID 6D13FC2B3EAEEDAE122B34C5FD8AF3DA\n",
      "BD_HOME 0\n",
      "BDORZ B490B5EBF6F3CD402E515D22BCDA1598\n",
      "BD_UPN 12314353\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "url = \"https://www.baidu.com/\"\n",
    "# options = webdriver.ChromeOptions()\n",
    "# options.set_headless()\n",
    "# driver = webdriver.Chrome(options=options)\n",
    "# driver.get(url)\n",
    "cookies = driver.get_cookies()\n",
    "for cookie in cookies:\n",
    "    print(cookie[\"name\"], cookie[\"value\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = webdriver.Chrome()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(\"https://www.facebook.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "post_element = driver.find_element_by_class_name(\"_3jk\")\n",
    "post_element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://www.facebook.com/qiao.fengchun/videos?lst=100012127725531%3A100012127725531%3A1523248948'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "driver.current_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://www.facebook.com/qiao.fengchun/videos/vb.100012127725531/439884463092474/?type=2&video_source=user_video_tab\n",
      "视频，时长：3分52秒\n",
      "https://www.facebook.com/qiao.fengchun/videos/vb.100012127725531/439885053092415/?type=2&video_source=user_video_tab\n",
      "视频，时长：1分7秒\n",
      "https://www.facebook.com/qiao.fengchun/videos/vb.100012127725531/439869996427254/?type=2&video_source=user_video_tab\n",
      "视频，时长：20秒\n"
     ]
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "page = driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "videos = soup.find_all(class_=\"fbPhotoStarGridElement\")\n",
    "for video in videos:\n",
    "    src = \"https://www.facebook.com\" + video.a.get(\"href\")\n",
    "    print(src)\n",
    "    print(video.a.get(\"aria-label\").replace(\" \", \"\"))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver  \n",
    "from selenium.webdriver.support.wait import WebDriverWait  \n",
    "from selenium.webdriver.support import expected_conditions as EC  \n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "# driver = webdriver.Chrome()\n",
    "url = \"https://www.facebook.com/search/str/hhh/keywords_users\"\n",
    "\n",
    "driver.get(url)\n",
    "while True:\n",
    "    try:\n",
    "        WebDriverWait(driver, 3, 0.5).until(\n",
    "            EC.presence_of_element_located((By.XPATH, xpath = \"//*[@id=\\\"browse_end_of_results_footer\\\"]/div/div\")))\n",
    "        break\n",
    "    except:\n",
    "        driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://www.facebook.com/qiao.fengchun/friends?lst=100012127725531%3A100012127725531%3A1523331339&source_ref=pb_friends_tl\"\n",
    "\n",
    "driver.get(url)\n",
    "while True:\n",
    "    try:\n",
    "        WebDriverWait(driver, 3, 0.5).until(\n",
    "            EC.presence_of_element_located((By.XPATH, \"//*[@id=\\\"timeline-medley\\\"]/div/div[2]/div[1]/div/div\")))\n",
    "        break\n",
    "    except:\n",
    "        driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "ename": "CannotSendRequest",
     "evalue": "Request-sent",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mCannotSendRequest\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-44-55a37088217b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     30\u001b[0m \u001b[1;31m# driver = webdriver.Chrome()\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     31\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msearch_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     33\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, url)\u001b[0m\n\u001b[0;32m    322\u001b[0m         \u001b[0mLoads\u001b[0m \u001b[0ma\u001b[0m \u001b[0mweb\u001b[0m \u001b[0mpage\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mcurrent\u001b[0m \u001b[0mbrowser\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    323\u001b[0m         \"\"\"\n\u001b[1;32m--> 324\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'url'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    325\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    326\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    308\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    309\u001b[0m         \u001b[0mparams\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_wrap_value\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 310\u001b[1;33m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    311\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    312\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\remote_connection.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, command, params)\u001b[0m\n\u001b[0;32m    464\u001b[0m         \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mutils\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdump_json\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    465\u001b[0m         \u001b[0murl\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'%s%s'\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_url\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 466\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcommand_info\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    467\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    468\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\remote_connection.py\u001b[0m in \u001b[0;36m_request\u001b[1;34m(self, method, url, body)\u001b[0m\n\u001b[0;32m    487\u001b[0m                 \u001b[0mbody\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    488\u001b[0m             \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 489\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_conn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparsed_url\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    490\u001b[0m                 \u001b[0mresp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_conn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetresponse\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    491\u001b[0m             \u001b[1;32mexcept\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mhttplib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mHTTPException\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msocket\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\http\\client.py\u001b[0m in \u001b[0;36mrequest\u001b[1;34m(self, method, url, body, headers)\u001b[0m\n\u001b[0;32m   1105\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1106\u001b[0m         \u001b[1;34m\"\"\"Send a complete request to the server.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1107\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_send_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1108\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1109\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_set_content_length\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbody\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\http\\client.py\u001b[0m in \u001b[0;36m_send_request\u001b[1;34m(self, method, url, body, headers)\u001b[0m\n\u001b[0;32m   1140\u001b[0m             \u001b[0mskips\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'skip_accept_encoding'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1141\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1142\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mputrequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmethod\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mskips\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1143\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1144\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;34m'content-length'\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mheader_names\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\http\\client.py\u001b[0m in \u001b[0;36mputrequest\u001b[1;34m(self, method, url, skip_host, skip_accept_encoding)\u001b[0m\n\u001b[0;32m    973\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__state\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_CS_REQ_STARTED\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    974\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 975\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mCannotSendRequest\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__state\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    976\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    977\u001b[0m         \u001b[1;31m# Save the method we use, we need it later in the response phase\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mCannotSendRequest\u001b[0m: Request-sent"
     ]
    }
   ],
   "source": [
    "# ID = 1,  //*[@id=\"BrowseResultsContainer\"]/div[1]/div\n",
    "# ID = 2,  //*[@id=\"BrowseResultsContainer\"]/div[2]/div\n",
    "# ID = 3,  //*[@id=\"BrowseResultsContainer\"]/div[3]/div\n",
    "# ID = 4,  //*[@id=\"BrowseResultsContainer\"]/div[4]/div\n",
    "# ID = 5,  //*[@id=\"BrowseResultsContainer\"]/div[5]/div\n",
    "\n",
    "# ID = 6,  //*[@id=\"pagelet_loader_u_ps_0_3_0_browse_result_below_fold]/div/div[1]/div\n",
    "# ID = 7,  //*[@id=\"pagelet_loader_u_ps_0_3_0_browse_result_below_fold\"]/div/div[2]/div\n",
    "# ID = 8,  //*[@id=\"pagelet_loader_u_ps_0_3_0_browse_result_below_fold\"]/div/div[3]/div\n",
    "# ID = 9, //*[@id=\"pagelet_loader_u_ps_0_3_0_browse_result_below_fold\"]/div/div[4]/div\n",
    "# ID = 10, //*[@id=\"pagelet_loader_u_ps_0_3_0_browse_result_below_fold\"]/div/div[5]/div\n",
    "# ID = 11, //*[@id=\"pagelet_loader_u_ps_0_3_0_browse_result_below_fold\"]/div/div[6]/div\n",
    "\n",
    "# ID = 11, //*[@id=\"fbBrowseScrollingPagerContainer0\"]/div/div[1]/div\n",
    "# ID = 12, //*[@id=\"fbBrowseScrollingPagerContainer0\"]/div/div[2]/div\n",
    "# ID = 13, //*[@id=\"fbBrowseScrollingPagerContainer0\"]/div/div[7]/div\n",
    "# ID = 22, //*[@id=\"fbBrowseScrollingPagerContainer0\"]/div/div[8]/div\n",
    "# ID = 23, //*[@id=\"fbBrowseScrollingPagerContainer1\"]/div/div[1]/div\n",
    "# ID = 30, //*[@id=\"fbBrowseScrollingPagerContainer1\"]/div/div[8]/div\n",
    "# //*[@id=\"fbBrowseScrollingPagerContainer1\"]/div/div[1]/div\n",
    "# //*[@id=\"fbBrowseScrollingPagerContainer0\"]/div/div[1]/div\n",
    "\n",
    "from selenium import webdriver\n",
    "# driver = webdriver.Chrome()\n",
    "\n",
    "search_url = \"https://www.facebook.com/search/str/\" + \"nihao\" + \"/keywords_users/\"\n",
    "# driver.get(search_url)\n",
    "\n",
    "user_number = 100\n",
    "# driver = webdriver.Chrome()\n",
    "\n",
    "driver.get(search_url)\n",
    "while True:\n",
    "    try:\n",
    "        WebDriverWait(driver, 3, 0.5).until(\n",
    "            EC.presence_of_element_located((By.XPATH, \"//*[@id=\\\"browse_end_of_results_footer\\\"]/div/div\")))\n",
    "        break\n",
    "    except:\n",
    "        driver.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")\n",
    "\n",
    "for index in range(user_number):\n",
    "    count = index + 1\n",
    "    if count <= 5:\n",
    "        xpath_str = \"//*[@id=\\\"BrowseResultsContainer\\\"]/div[\" + str(count) + \"]/div\"\n",
    "    elif 5 < count <= 11:\n",
    "        xpath_str = \"//*[@id=\\\"u_ps_fetchstream_0_3_0_browse_result_below_fold\\\"]/div/div[\" + str(count - 5) + \"]/div\"\n",
    "    else:\n",
    "        pager = (count - 12) // 6\n",
    "        number = (count - 12) % 6 + 1\n",
    "        \n",
    "        xpath_str = \"//*[@id=\\\"fbBrowseScrollingPagerContainer\" +  str(pager) + \"\\\"]/div/div[\" + str(number) + \"]/div\"\n",
    "        element = driver.find_element_by_xpath(xpath=xpath_str)\n",
    "        print(element.get(\"data-bt\"))\n",
    "    \n",
    "\n",
    "# if user_number <= 6:\n",
    "#     for i in range(6):\n",
    "#         i += 1\n",
    "#         xpath_str = \"//*[@id=\\\"BrowseResultsContainer\\\"]/div[\" + str(i) + \"]/div\"\n",
    "#         element = driver.find_element_by_xpath(xpath_str)\n",
    "#         print(dir(element))\n",
    "#         print(element.get_attribute(\"data-bt\"))\n",
    "\n",
    "#     refresh_times = user_number // 5\n",
    "#     self.page_refresh(refresh_times)\n",
    "#     page_source = self.driver.page_source\n",
    "#     soup = BeautifulSoup(page_source, self.soup_type)\n",
    "\n",
    "#     # 获取user homepage列表\n",
    "#     contents = soup.find_all(class_=self.user_item_class_name)\n",
    "#     for content in contents:\n",
    "#         user_url_name = content.find(class_=self.user_info_class_name)\n",
    "#         homepage_url = user_url_name.get(\"href\")\n",
    "#         if utils.url_type_judge(homepage_url) == 1:\n",
    "#             homepage_url = homepage_url.replace(\"?ref=br_rs\", \"\")\n",
    "#         if utils.url_type_judge(homepage_url) == 2:\n",
    "#             homepage_url = homepage_url.replace(\"&ref=br_rs\", \"\")\n",
    "\n",
    "#         user_name = user_url_name.text\n",
    "#         location = content.find(class_=self.location_class_name).text\n",
    "#         user_info_dict = content.find(class_=self.id_class_name).get(\"data-bt\")\n",
    "#         pattern = re.compile(r\"\\\"id\\\":\\d+\")\n",
    "#         user_id = pattern.findall(user_info_dict)[0].split(\"\\\"id\\\":\")[-1]\n",
    "\n",
    "#         self.user_info_search.append([user_name, user_id, location, homepage_url])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "WebDriverException",
     "evalue": "Message: unknown error: cannot determine loading status\nfrom unknown error: cannot determine loading status\nfrom disconnected: unable to send message to renderer\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mWebDriverException\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-5e0b8705ffd3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0mdriver\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwebdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mChrome\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msearch_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0muser_number\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mget\u001b[1;34m(self, url)\u001b[0m\n\u001b[0;32m    322\u001b[0m         \u001b[0mLoads\u001b[0m \u001b[0ma\u001b[0m \u001b[0mweb\u001b[0m \u001b[0mpage\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mthe\u001b[0m \u001b[0mcurrent\u001b[0m \u001b[0mbrowser\u001b[0m \u001b[0msession\u001b[0m\u001b[1;33m.\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    323\u001b[0m         \"\"\"\n\u001b[1;32m--> 324\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'url'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0murl\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    325\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    326\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    310\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    311\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 312\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    313\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    314\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    240\u001b[0m                 \u001b[0malert_text\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malert_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 242\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mWebDriverException\u001b[0m: Message: unknown error: cannot determine loading status\nfrom unknown error: cannot determine loading status\nfrom disconnected: unable to send message to renderer\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "\n",
    "search_url = \"https://www.facebook.com/search/str/\" + \"hhhhhh\" + \"/keywords_users/\"\n",
    "\n",
    "user_number = 100\n",
    "driver = webdriver.Chrome()\n",
    "\n",
    "driver.get(search_url)\n",
    "if user_number is None:\n",
    "    while True:\n",
    "        try:\n",
    "            WebDriverWait(driver, 3, 0.5).until(\n",
    "                EC.presence_of_element_located((By.XPATH, \"//*[@id=\\\"browse_end_of_results_footer\\\"]/div/div\")))\n",
    "            break\n",
    "        except:\n",
    "            driver.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'isset' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-674e2da52df1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0misset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwebdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mchrome\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwebdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mWebDriver\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m         \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"http://www.facebook.com\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'isset' is not defined"
     ]
    }
   ],
   "source": [
    "print(isset(driver))\n",
    "if isinstance(driver, webdriver.chrome.webdriver.WebDriver):\n",
    "    try:\n",
    "        driver.get(\"http://www.facebook.com\")\n",
    "    except:\n",
    "        print(\"当前浏览器窗口已关闭, 请重新初始化\")\n",
    "        del driver\n",
    "else:\n",
    "    driver = webdriver.Chrome()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "ename": "WebDriverException",
     "evalue": "Message: chrome not reachable\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mWebDriverException\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-13-7e73d96589b8>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      8\u001b[0m \u001b[0mdriver2\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mwebdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mRemote\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mexecutor_url\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdesired_capabilities\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mdriver2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msession_id\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msession_id\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 10\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcurrent_url\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mcurrent_url\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    658\u001b[0m             \u001b[0mdriver\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcurrent_url\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    659\u001b[0m         \"\"\"\n\u001b[1;32m--> 660\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCommand\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mGET_CURRENT_URL\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'value'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    661\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    662\u001b[0m     \u001b[1;33m@\u001b[0m\u001b[0mproperty\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\webdriver.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    310\u001b[0m         \u001b[0mresponse\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcommand_executor\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexecute\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdriver_command\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    311\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mresponse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 312\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0merror_handler\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcheck_response\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    313\u001b[0m             response['value'] = self._unwrap_value(\n\u001b[0;32m    314\u001b[0m                 response.get('value', None))\n",
      "\u001b[1;32mc:\\program files\\python35\\lib\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py\u001b[0m in \u001b[0;36mcheck_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    240\u001b[0m                 \u001b[0malert_text\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvalue\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'alert'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'text'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    241\u001b[0m             \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0malert_text\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 242\u001b[1;33m         \u001b[1;32mraise\u001b[0m \u001b[0mexception_class\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mscreen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstacktrace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    243\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    244\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_value_or_default\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mobj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdefault\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mWebDriverException\u001b[0m: Message: chrome not reachable\n  (Session info: chrome=65.0.3325.181)\n  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 6.1.7601 SP1 x86_64)\n"
     ]
    }
   ],
   "source": [
    "from selenium.webdriver import Remote\n",
    "from selenium.webdriver.chrome import options\n",
    "from selenium.common.exceptions import InvalidArgumentException\n",
    "\n",
    "executor_url = driver.command_executor._url\n",
    "session_id = driver.session_id\n",
    "\n",
    "driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities={})\n",
    "driver2.session_id = session_id\n",
    "print(driver2.current_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    type(eval(var))\n",
    "except BaseException:\n",
    "    print(0)\n",
    "else:\n",
    "    print(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': 'H_PS_PSSID', 'domain': '.baidu.com', 'value': '1462_26080_21118_22160', 'httpOnly': False, 'secure': False, 'path': '/'}, {'secure': False, 'name': 'BAIDUID', 'domain': '.baidu.com', 'value': '51E742A9EA829D8C705D50AF2FCE6CC3:FG=1', 'httpOnly': False, 'path': '/', 'expiry': 3670895661.878873}, {'secure': False, 'name': 'PSTM', 'domain': '.baidu.com', 'value': '1523412021', 'httpOnly': False, 'path': '/', 'expiry': 3670895661.878939}, {'secure': False, 'name': 'BIDUPSID', 'domain': '.baidu.com', 'value': '51E742A9EA829D8C705D50AF2FCE6CC3', 'httpOnly': False, 'path': '/', 'expiry': 3670895661.878915}, {'name': 'BD_HOME', 'domain': 'www.baidu.com', 'value': '0', 'httpOnly': False, 'secure': False, 'path': '/'}, {'secure': False, 'name': 'BD_UPN', 'domain': 'www.baidu.com', 'value': '12314353', 'httpOnly': False, 'path': '/', 'expiry': 1524276666}]\n"
     ]
    }
   ],
   "source": [
    "# driver = webdriver.Chrome()\n",
    "driver.get(\"http://www.baidu.com\")\n",
    "cookie = driver.get_cookies()\n",
    "print(cookie)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# driver = webdriver.Chrome()\n",
    "# driver.get(\"https://www.facebook.com/\")\n",
    "if driver.current_url != \"https://www.facebook.com/\":\n",
    "    driver.get(\"https://www.facebook.com/\")\n",
    "else:\n",
    "    pass\n",
    "post_element = driver.find_element_by_class_name(\"_3jk\")\n",
    "post_element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_xpath_str(number):\n",
    "    count = number + 1\n",
    "    if count <= 5:\n",
    "        xpath_str = \"//*[@id=\\\"BrowseResultsContainer\\\"]/div[\" + str(count) + \"]/div\"\n",
    "    elif 5 < count <= 11:\n",
    "        xpath_str = \"//*[@id=\\\"u_ps_fetchstream_0_3_0_browse_result_below_fold\\\"]/div/div[\" + str(count - 5) + \"]/div\"\n",
    "    else:\n",
    "        pager = (count - 12) // 6\n",
    "        number = (count - 12) % 6 + 1\n",
    "        \n",
    "        xpath_str = \"//*[@id=\\\"fbBrowseScrollingPagerContainer\" +  str(pager) + \"\\\"]/div/div[\" + str(number) + \"]/div\"\n",
    "        element = driver.find_element_by_xpath(xpath=xpath_str)\n",
    "        print(element.get(\"data-bt\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "user_search_class_name: _4p2o\n",
      "user_id: 100000372970376\n",
      "homepage: https://www.facebook.com/RealIsisTaylorPage?ref=br_rs\n",
      "user_name: Tiffany Taylor\n",
      "about: Penthouse\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Tiffany Taylor',\n",
       " 100000372970376,\n",
       " 'https://www.facebook.com/RealIsisTaylorPage?ref=br_rs',\n",
       " 'Penthouse']"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from bs4 import BeautifulSoup\n",
    "import json\n",
    "\n",
    "# 通过BrowseResultsContainer找到class=_4p2o\n",
    "element = driver.find_element_by_xpath(\"//*[@id=\\\"BrowseResultsContainer\\\"]/div[1]\")\n",
    "user_search_class_name = element.get_attribute(\"class\")\n",
    "print(\"user_search_class_name: %s\" % user_search_class_name)\n",
    "\n",
    "number = 100\n",
    "if number is None:\n",
    "    \n",
    "\n",
    "page = driver.page_source\n",
    "soup = BeautifulSoup(page, \"html.parser\")\n",
    "\n",
    "# 获取user id\n",
    "items = soup.find_all(class_=user_search_class_name)\n",
    "item = items[6]\n",
    "get_user_info(item)\n",
    "\n",
    "# print(items.find(class_=\"clearfix\").a.get(\"href\"))\n",
    "# print(items.find(class_=\"clearfix\").div.get(\"class\"))\n",
    "# print(items.find(class_=\"clearfix\").div.find(class_=\"clearfix\").a.text)\n",
    "# print(type(data_bt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "_4p2o\n"
     ]
    }
   ],
   "source": [
    "print(print(items.find(class_=\"clearfix\").a.get(\"href\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "def str2dict(string):\n",
    "    \"\"\"\n",
    "    将字符串转为字典变量，从网页源代码解析出的部分字典类型的变量以字符串形式存储，因此需要先对其进行转换，再取出对应元素\n",
    "    :param string: 带转换的字典形式的字符串\n",
    "    :return:\n",
    "        result_dict: 转换后的字典变量\n",
    "    \"\"\"\n",
    "    result_dict = json.loads(string, encoding=\"UTF-8\")\n",
    "\n",
    "    return result_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'null' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-228-c104cec84845>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m\"id\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m100000819426635\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"rank\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"abtest_version\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"abtest_params\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m\"abtest_version\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"origin\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"A\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"ranker\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"section\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"main_column\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"owner_id\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"sub_id\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"browse_location\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"query_data\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m{\u001b[0m\u001b[1;34m\"q\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"taylor\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"ref\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;34m\"eyJzaWQiOiIwLjgzNTA1OTAzOTEzMjg4NjciLCJyZWYiOiJ0b3BfZmlsdGVyIn0=\"\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"is_headline\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[0mfalse\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mb\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mencoding\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"UTF-8\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mb\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'null' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_user_info(item):\n",
    "    data_be_str = item.div.get(\"data-bt\")\n",
    "    user_id = str2dict(data_be_str)[\"id\"]\n",
    "    print(\"user_id: %s\" % user_id)\n",
    "    \n",
    "    # 获取user homepage url\n",
    "    user_info = item.find(class_=\"clearfix\")\n",
    "    user_homepage_url =  user_info.a.get(\"href\")\n",
    "    print(\"homepage: %s\" % user_homepage_url)\n",
    "\n",
    "    user_name_block = user_info.div.find(class_=\"clearfix\").find_all(\"div\")\n",
    "    user_name_class_name = user_name_block[-1].a.get(\"class\")[0]\n",
    "    user_name = block[-1].a.text\n",
    "    print(\"user_name: %s\" % user_name)\n",
    "\n",
    "    about_items = user_info.find_all(\"div\")\n",
    "    about_class = about_items[11].find_all(\"div\")\n",
    "    \n",
    "    try:\n",
    "        about = about_class[5].text\n",
    "    except:\n",
    "        about = None\n",
    "    print(\"about: %s\" % about)\n",
    "    \n",
    "    return [user_name, user_id, user_homepage_url, about]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def page_refresh_to_bottom(item):\n",
    "    \"\"\"\n",
    "    页面刷新\n",
    "    :param item:\n",
    "    :return:\n",
    "    \"\"\"\n",
    "    if item == \"users\":\n",
    "        xpath = \"//*[@id=\\\"browse_end_of_results_footer\\\"]/div/div\"\n",
    "    else:\n",
    "        xpath = \"//*[@id=\\\"timeline-medley\\\"]/div/div[2]/div[1]/div/div\"\n",
    "\n",
    "    while True:\n",
    "        # noinspection PyBroadException\n",
    "        try:\n",
    "            WebDriverWait(self.driver, 3, 0.5).until(\n",
    "                EC.presence_of_element_located((By.XPATH, xpath)))\n",
    "            break\n",
    "        except BaseException:\n",
    "            driver.execute_script('window.scrollTo(0, document.body.scrollHeight);')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
